// 修改绑定邮箱，身份验证一
$(function() {

	// 发送验证码
	var sendBtn = $("#getEmailSecurityCodeBtn");
	sendBtn.click(function() {
		settime();
		var email = $("#emailSpan").text();// 获取输入的邮箱
		console.log("【-email-】" + email)
		$.ajax({
			type : 'post',
			url : '/tenement/getEmailVerifyCode.action',
			data : email,
			contentType : "application/json;charset=utf-8",
			success : function(msg) {
				console.log("【后台传来的邮箱验证码】:" + msg);
			},
			error : function(msg) {
				console.log("系统异常");
			}
		})
	})
	// 发送验证码倒计时60s
	var countdown = 10;
	function settime() {
		if (countdown == 0) {
			sendBtn.html("获取验证码");
			if ($("#emailFlag").attr("value") == "true") {
				mailBtnYes();
			} else {
				mailBtnNo();
			}
			countdown = 10;
			return;
		} else {
			sendBtn.html("重新发送(" + countdown + ")");
			mailBtnNo();
			countdown--;
		}
		setTimeout(settime, 1000);
	}
	// 获取验证码按钮可以点击
	function mailBtnYes() {
		sendBtn.removeClass("disabled");
		sendBtn.attr("disabled", false);
	}
	// 获取验证码按钮不可点击
	function mailBtnNo() {
		sendBtn.attr("disabled", true);
		sendBtn.addClass("disabled");
	}

	// 绑定邮箱数据提交 u_email emailCode
	function modifyEmail() {
		$("#codeTipNull").css("display", "none");
		$("#codeTipError").css("display", "none");
		var u_email = $("#emailSpan").text();
		var emailCode = $('#emailCodeIpt').val();// 用户输入的手机验证码
		var data = {
			"u_email" : u_email,
			"emailCode" : emailCode
		};
		console.log(data);
		$
				.ajax({
					type : 'post',
					url : '/tenement/verifyEmailCode.action',
					data : JSON.stringify(data),
					contentType : "application/json;charset=utf-8",
					success : function(msg) {
						console.log("【msg】" + msg);
						if (msg == "1") {
							$("#codeTipError").css("display", "none");
							$("#codeTipNull").css("display", "none");
							window.location = "/tenement/toPersonModifyEmailHtml2.action";
						} else {
							$("#codeTipNull").css("display", "none");
							$("#codeTipError").css("display", "block");// 验证码错误
						}
					},
					error : function(msg) {
						console.log("【系统异常】");
					}
				});

	}
	$("#nextBtn").click(function() {
		if (verifyEmailCodeIpt()) {
			modifyEmail();
		} else {
			console.log("【数据提交失败。。。】");
		}
	})

	function verifyEmailCodeIpt() {
		var code = $('#emailCodeIpt').val();// 获取输入的验证码的值
		if (code != "") {
			$("#codeTipNull").css("display", "none");// 验证码为空
			$("#codeTipError").css("display", "none");// 验证码错误
			return true;// 验证码不为空才能提交
		} else {
			$("#codeTipError").css("display", "none");// 提示用户验证码错误
			$("#codeTipNull").css("display", "block");// 提示用户验证码为空
			return false;
		}
	}
})